package com.shujia.spark.core

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo14mapValues {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Demo13Sort")

    val sc = new SparkContext(conf)

    val kvRDD: RDD[(String, Int)] = sc.textFile("data/score.txt")
      .map(_.split(","))
      .filter(_.length == 3)
      .map {
        case Array(sid: String, _: String, sco: String) =>
          (sid, sco.toInt)
      }

    //统计总分
    val sumScoRDD: RDD[(String, Int)] = kvRDD.reduceByKey(_ + _)

    /**
     * mapValues： 对value做处理，可以不变
     *
     */


    val mapValuesRDD: RDD[(String, Int)] = sumScoRDD.mapValues(scp => scp / 10)
    mapValuesRDD.foreach(println)

    //等同于
    //sumScoRDD.map(kv => (kv._1, kv._2 / 10))

  }

}
